/**
 * author:shinion
 * date:2019-11-01
 * 功能：
 *      1.检测809状态数据功能模块
 * 
 */


var Jt809_status = {};

Jt809_status.debug = true;
Jt809_status.parDivId = '';
Jt809_status.userList = [];//用户列表，即在列表中的用户才进行809状态监测
Jt809_status.platformList = [];//809平台列表，在列表中的平台才会进行状态监测

Jt809_status.typeMain = 'mainLink';//主链路类型
Jt809_status.typeSub = 'subLink';//从链路类型

Jt809_status.checkTimerName = 'checkAllPlatformStatusTimer850302';//校验定时器名称
Jt809_status.checkTimerValue = '20s';//校验定时器间隔时间

Jt809_status.baseSlctUrl = globalConfig.webPath + '/basicSlctAction/basicSelectData.action';
Jt809_status.queryId = 'jt809Command.getLastStatusData';
Jt809_status.timeSpanValue = 60;//状态时间间隔，间隔超过60秒，则认为当前状态为false

Jt809_status.divStatus0 = '<div class="ui-icon icon-alarm" style="width:16px;height:16px;"></div>';//状态为0图标
Jt809_status.divStatus1 = '<div class="ui-icon icon-ok" style="width:16px;height:16px;"></div>';//状态为1图标

Jt809_status.divIdAdd = '_status_div_';//控件中间字符串


///////////////////////////////////////////初始化方法

/*
接口，初始化状态监测功能
*/
Jt809_status.init = function(parDivId,userList,platformList){
    // this.outDebug("初始化开始...........");
    // this.outDebug("parDivId:" + parDivId);
    // this.outDebug("userList :" , userList);
    // this.outDebug("platformList:" , platformList);
    // this.outDebug("userInfo:" ,globalConfig.userInfo);
    //保存数据
    this.parDivId = parDivId;
    this.userList = userList;
    this.platformList = platformList;

    //开始初始化
    if(this.userList.indexOf(globalConfig.userInfo.userType) > -1){
        //允许进行状态获取
        this.startService();
    }
}

//开始进行状态获取服务服务
Jt809_status.startService = function(){
    this.showParDiv();
    this.checkAllPlatformStatus();//校验所有平台状态
}


//显示容器控件
Jt809_status.showParDiv = function(){
    let obj = document.getElementById(this.parDivId);
    if(obj != null){
        obj.style.display = "";
    }
}

/////////////////////////////////////////////////校验平台状态

//统一调用接口，校验所有平台的状态
Jt809_status.checkAllPlatformStatus = function(){
    let that = this;
    $(document).everyTime(that.checkTimerValue,that.checkTimerName,function(){
        try {
            let len = that.platformList.length;
            if(len > 0){
                for(let i=0 ; i<len ; i++){
                    let name = that.platformList[i];
                    if(name != ""){
                        //开始校验单个平台的单个链路状态
                        that.checkOneLinkStatus(name,that.typeMain);//校验主链路状态
                        that.checkOneLinkStatus(name,that.typeSub);//校验从链路状态
                    }
                }
            }
        } catch (error) {
            DataUtil.outError("校验平台状态失败：" + error);
        }
    });
}

//校验单个链路的状态
Jt809_status.checkOneLinkStatus = function(name,type){
    let that = this;
    let start = new Date().getTime() - that.timeSpanValue * 1000;
    let param = {
        name:name,
        type:type,
        start:new Date(start),
        queryId:that.queryId,
    };
    // that.outDebug("param:" , param);
    Utility.getAjaxData(that.baseSlctUrl,JSON.stringify(param),function(data){
        if(data.success){
            if(data.data && data.data.length > 0){
                //有数据
                let item = data.data[0];
                // that.outDebug("状态数据：name = " + item.name + " , type=" + item.type + " , status =" + item.status + " , endTime = " + item.endTime);
                that.showLinkStatus(item.name,item.type,item.status + '');
            }else{
                that.showLinkStatus(name,type,'0');
            }
        }else{
            that.showLinkStatus(name,type,'0');
            DataUtil.outError("获取单个链路的状态数据失败："+ that.message);
        }
    });
}

//设置链路状态
Jt809_status.showLinkStatus = function(name,type,status){
    let id = name + this.divIdAdd + type;
    if(status == '0'){
        $("#" + id).html(this.divStatus0);
        this.playAlarmSound(name,type,status);
    }else if(status == '1'){
        $("#" + id).html(this.divStatus1);
    }
}

//播放报警声音
Jt809_status.playAlarmSound = function(name,type,status){
    AlarmSound.play809();
}







///////////////////////////////////////////公用方法
Jt809_status.outDebug = function (msg, data) {
    if (this.debug == true) {
        DataUtil.outDebug(msg, data);
    }
}